package cn.px.rest.controller.ticket;

import cn.px.common.util.R;
import cn.px.dao.dto.ticket.TicketSafeDTO;
import cn.px.dao.entity.ticket.TicketSafe;
import cn.px.service.ticket.TicketSafeService;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.ticket.ExportTicketSafeVO;
import cn.px.dao.vo.query.ticket.TicketSafeVO;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.rest.log.annotation.SysLog;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 两票安全措施 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/ticket/ticket-safes")
public class TicketSafeController {

  private final  TicketSafeService ticketSafeService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<TicketSafe> getById(@PathVariable("id") String id){
      return new R<>(ticketSafeService.getModelById(id));
  }

  /**
   * 新增记录
   *
   * @param ticketSafeDTO 两票安全措施DTO
   * @return R
   */
  @SysLog("新增两票安全措施")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('ticket-ticket-saves-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody TicketSafeDTO ticketSafeDTO) {
      return new R<>(ticketSafeService.saveModelByDTO(ticketSafeDTO));
  }

  /**
   * 修改记录
   *
   * @param ticketSafeDTO 两票安全措施DTO
   * @return R
   */
  @SysLog("修改两票安全措施")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('ticket-ticket-saves-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody TicketSafeDTO ticketSafeDTO) {
      ticketSafeService.modModelByDTO(ticketSafeDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除两票安全措施")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('ticket-ticket-saves-del')")
  public R removeById(@PathVariable String id){
    ticketSafeService.deleteByPrimaryKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除两票安全措施")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('ticket-ticket-saves-del')")
    public R removeIds(@RequestBody List<String> ids){
        ticketSafeService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param ticketSafeDTO 两票安全措施DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<TicketSafeVO>> query(TicketSafeDTO ticketSafeDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(ticketSafeService.selectPageList(ticketSafeDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param ticketSafeDTO 两票安全措施DTO
   * @return R
   */
  @GetMapping("")
  public R query(TicketSafeDTO ticketSafeDTO) {
      return new R<>(ticketSafeService.getModelListByDTO(ticketSafeDTO));
  }

  /**
     * 两票安全措施导出
     *
     * @param ticketSafeDTO 两票安全措施DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("两票安全措施导出")
  @PreAuthorize("@pms.hasPermission('ticket-ticket-saves-export')")
  public void export(HttpServletResponse response, TicketSafeDTO ticketSafeDTO) {
    List<TicketSafe> list = ticketSafeService.getModelListByDTO(ticketSafeDTO);
    ExcelUtil.exportResponseDict(response, ExportTicketSafeVO.class, BeanConverterUtil.copyListProperties(list, ExportTicketSafeVO.class), "两票安全措施");
  }

}
